# -*- coding: utf-8 -*-
from django.contrib.auth.models import User
from django.db import models
from pytils.translit import translify
from django.utils.translation import ugettext_lazy as _

import os
import uuid
import time

def upload_path(instance,filename):
    return u"uploads/%s/%s" % (time.strftime('%Y-%m'),translify(filename))

class File(models.Model):
    name = models.CharField(max_length=255,verbose_name=_('name'),blank=True)
    downloads = models.IntegerField(verbose_name=_('download count'),default=0,editable=False)
    file = models.FileField(upload_to=upload_path,verbose_name=_('file'))
    owner = models.ForeignKey(User, verbose_name=_('owner'))
    avalible = models.BooleanField(verbose_name=_('avalible'), default=True)
    uuid = models.CharField(unique=True, max_length=36, verbose_name=_('file uuid'),default=uuid.uuid4,editable=False)

    def delete(self, *args, **kwargs):
        super(File, self).delete(*args, **kwargs)
        os.unlink(self.file.path)

    def filepath(self):
        return os.path.abspath(self.file.name)

    class Meta:
        verbose_name = _('file')
        verbose_name_plural = _('files')

    def unicode(self):
        return self.name

